package com.wangxq.yd_login.login.mvp.ui.activity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.PopupMenu;
import androidx.core.app.ActivityCompat;

import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import com.tencent.mmkv.MMKV;
import com.wangxq.mylibrary.dialog.DialogTools;
import com.wangxq.yd_login.R;
import com.wangxq.yd_login.login.di.component.DaggerLoginComponent;
import com.wangxq.yd_login.login.di.module.LoginModule;
import com.wangxq.yd_login.login.mvp.contract.LoginContract;
import com.wangxq.yd_login.login.mvp.presenter.LoginPresenter;
import com.zlx.module_base.database.MMkvHelper;
import com.zlx.widget.text.ClearEditText;


import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;


public class LoginActivity extends BaseActivity<LoginPresenter> implements LoginContract.View {
    @Inject
    LoginPresenter mPresenter;
    @BindView(R.id.imageView2)
    ImageView imageView2;
    @BindView(R.id.textView1)
    TextView textView1;
    @BindView(R.id.usernameText)
    ClearEditText usernameText;
    @BindView(R.id.ivDelete)
    ImageView ivDelete;
    @BindView(R.id.input_account)
    LinearLayout inputAccount;
    @BindView(R.id.passwordText)
    ClearEditText passwordText;
    @BindView(R.id.loginBtn)
    Button loginBtn;
    @BindView(R.id.rememberAccount)
    CheckBox rememberAccount;
    @BindView(R.id.settingTxv)
    TextView settingTxv;
    @BindView(R.id.version)
    TextView version;
    List<String> users = new ArrayList<>();
    PopupMenu popupMenu;
    @BindView(R.id.ivSpring)
    ImageView ivSpring;
    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerLoginComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .loginModule(new LoginModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_login; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
//        StateUtils.setLightStatusBar(this, true);// 设置状态栏字体颜色为黑色
        Log.e("尺寸", ScreenUtils.getScreenWidth() + "" + ScreenUtils.getScreenHeight() + ScreenUtils.getScreenDensityDpi());
        popupMenu = new PopupMenu(this, inputAccount);
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                usernameText .setText(item.getTitle());
                InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(inputAccount.getWindowToken(), 0);
                }
                popupMenu.dismiss();
                return false;
            }
        });
//        version.setText("V" + BuildConfig.VERSION_NAME);
        usernameText.setText("SH001");
        passwordText.setText("123");

        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
            if (!checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA,Manifest.permission.REQUEST_INSTALL_PACKAGES,Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS,Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_PERMISSION_STORAGE);
            }
        }
        MMKV mmkv=MMKV.defaultMMKV();
        mmkv.putString("baseUrl","http://139.9.246.226:8088/");
    }
    public  int REQUEST_PERMISSION_STORAGE = 0x01;
    private boolean checkPermission(@NonNull String permission) {
        return ActivityCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED;
    }
    @Override
    public void showLoading() {
        DialogTools.INSTANCE.showLoading(getSupportFragmentManager());
    }

    @Override
    public void hideLoading() {
        DialogTools.INSTANCE.dismissLoading();
    }

    @Override
    public void showMessage(@NonNull String message) {
        ToastUtils.showShort(message);
    }
    @OnClick(R.id.loginBtn)
    void login() {
//        toMainActivity();
//        Intent intent = new Intent(LoginActivityActivity.this, ClassifierActivity.class);
//        Intent intent = new Intent(LoginActivityActivity.this, CameraActivity.class);
//        startActivityForResult(intent, REQUEST_CODE_SELECT);
        if (usernameText.getText() == null || StringUtils.isTrimEmpty(usernameText.getText().toString())) {
            ToastUtils.showShort("请输入用户名！");
            return;
        }
        if (passwordText.getText() == null || StringUtils.isTrimEmpty(passwordText.getText().toString())) {
            ToastUtils.showShort("请输入密码！");
            return;
        }
        showLoading();
        MMKV mmkv=MMKV.defaultMMKV();
        String baseUrl = mmkv.getString("baseUrl","")+"DTCG/app/appLogin.action";
        if (rememberAccount.isChecked()) {
            mPresenter.Login(baseUrl,usernameText.getText().toString(), passwordText.getText().toString(), true);
        } else {
            mPresenter.Login(baseUrl,usernameText.getText().toString(), passwordText.getText().toString(), false);
        }

    }
    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }
    @OnClick(R.id.settingTxv)
    void toSetting() {
//        ArmsUtils.startActivity(new Intent(this, ChangeServerActivity.class));
    }
    @OnClick(R.id.ivSpring)
    void ShowPopup(){
        if(users.size()>0){
            android.view.Menu menu_more = popupMenu.getMenu();
            menu_more.clear();
            int size = users.size();
            for (int i = 0; i < size; i++) {
                menu_more.add(android.view.Menu.NONE,  i, i, users.get(i));
            }
            popupMenu.show();
        }else {
            ToastUtils.showShort("无保存用户信息");
        }
    }

    @Override
    public void toMainActivity() {
        MMKV mmkv=MMKV.defaultMMKV();
        String mainClass= mmkv.decodeString("mainClass");
        try {
            Class mainClassInfo=Class.forName(mainClass);
            Intent intent=new Intent();
            intent.setClass(LoginActivity.this,mainClassInfo);
            startActivity(intent);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }
}
